//-----------------------------------------------------------------------
// <copyright file="IMAddressCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Collections.Specialized;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents a collection of instant messaging addresses.
    /// </summary>
    public sealed class IMAddressCollection : Collection
    {
        internal IMAddressCollection(object obj, User user)
            : base(obj, user)
        {
        }

        /// <summary>
        /// Adds an instant messaging address to the collection.
        /// </summary>
        /// <param name="address">The address to add.</param>
        public void Add(IMAddress address)
        {
            if (address == null)
            {
                throw ExceptionHelper.CreateArgumentNullException("address");
            }

            if (this.Contains(address))
            {
                throw ExceptionHelper.CreateArgumentException("IMAddress already belongs to the collection", "address");
            }

            this.Entity.Invoke("Add", address.Entity);
        }

        /// <summary>
        /// Removes an instant messaging address from the collection.
        /// </summary>
        /// <param name="address">The address to remove.</param>
        public void Remove(IMAddress address)
        {
            if (address == null)
            {
                throw ExceptionHelper.CreateArgumentNullException("address");
            }

            if (!this.Contains(address))
            {
                throw ExceptionHelper.CreateArgumentException("IMAddress does not belong to the collection", "address");
            }

            this.Entity.Invoke("Remove", address.Entity);
        }

        /// <summary>
        /// Gets the <c>IMAddress</c> at the specific index.
        /// </summary>
        /// <param name="index">The zero-based index of the <c>IMAddress</c> to get.</param>
        /// <returns>The <c>IMAddress</c> at the specified index.</returns>
        public IMAddress this[int index]
        {
            get { return (IMAddress)this.GetItem(index); }
        }

        internal override object FindOrCreate(MessengerEntity entity)
        {
            return this.User.IMAddressFactory.FindOrCreate(entity);
        }
    }
}
